var express = require('express');
var user = require("../models/user.js");
var info = require("../models/info.js");
var ec = require("../models/errorController.js");
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

//管理员注册
router.post('/user/signup', function  (req, res) {
  var data = req.body;
  var response;
  if(data.signupcode != "ying0ying0ying"){
    response = ec.addErrorInfo(409);
    res.send(response);
    return;
  }
  if(!data.username || !data.password || !data.name || !data.place){
    response = ec.addErrorInfo(203);
    res.send(response);
    return;
  }
  if(data.password.length < 6){
    response = ec.addErrorInfo(408);
    res.send(response);
    return;
  }
  user.signup(data.username, data.password, data.name, data.place, function(errorCode){
    var response = ec.addErrorInfo(errorCode);
    res.send(response);
  });
});

//管理员登陆
router.post('/user/login', function  (req, res) {
  var data = req.body;
  var response;
  if(!data.username || !data.password){
    response = ec.addErrorInfo(203);
    res.send(response);
    return;
  }
  user.login(data.username, data.password, function(errorCode, result){
    var response = ec.addErrorInfo(errorCode, result);
    res.send(response);
  });
});

//管理员登出
router.post('/user/logout', function  (req, res) {
  var data = req.body;
  var response;
  if(!data.token){
    response = ec.addErrorInfo(203);
    res.send(response);
    return;
  }
  user.logout(data.token, function(errorCode){
    var response = ec.addErrorInfo(errorCode);
    res.send(response);
  });
});

//新社员注册
router.post('/info/signup', function(req, res){
  var data = req.body;
  var response;
  if(!data.name || !data.grade || !data.department || !data.dormitory || !data.tel || !data.qq){
    response = ec.addErrorInfo(203);
    res.send(response);
    return;
  }
  info.signup(data.name, data.grade, data.department, data.dormitory, data.tel, data.qq, function(errorCode, id){
    var response = ec.addErrorInfo(errorCode);
    response.id = id;
    res.send(response);
  });
});

//信息搜索
router.post('/info/search', function(req, res){
  var data = req.body;
  var response;
  if(!data.token || !data.search){
    response = ec.addErrorInfo(203);
    res.send(response);
    return;
  }
  user.checkToken(data.token, function(errorCode){
    if(ec.getResult(errorCode)){
      info.search(data.search, function(errorCode, result){
        var r = {result: result};
        var response = ec.addErrorInfo(errorCode, r);
        res.send(response);
      });
    }else{
      var response = ec.addErrorInfo(errorCode);
      res.send(response);
    }
  });

});

//信息更新
router.post('/info/update', function(req, res){
  var data = req.body;
  var response;
  if(!data.token || !data.id || !data.data){
    response = ec.addErrorInfo(203);
    res.send(response);
    return;
  }
  user.checkToken(data.token, function(errorCode, userDetail){
    if(ec.getResult(errorCode)){
      info.update(data.id, JSON.parse(data.data),userDetail, function(errorCode){
        var response = ec.addErrorInfo(errorCode);
        res.send(response);
      });
    }else{
      var response = ec.addErrorInfo(errorCode);
      res.send(response);
    }
  });
});

router.post('/info/file', function(req, res){
  var data = req.body;
  user.checkToken(data.token, function(errorCode){
    if(ec.getResult(errorCode)){
      info.saveToCvs(-1, function(errorCode, file){
        res.setHeader('Content-Description', 'File Transfer');
        res.setHeader('Content-Type', 'application/csv; charset=utf-8');
        res.setHeader('Content-Type', 'application/force-download');
        res.setHeader('Content-Type', 'application/download');
        res.setHeader('Content-Disposition', 'attachment; filename=statistics.csv');
        res.setHeader('Expires', '0');
        res.setHeader('Cache-Control', 'must-revalidate');
        res.send('\uFEFF' + file);
      });
    }else{
      var response = ec.addErrorInfo(errorCode);
      res.render('index', { title: '下载失败' + errorCode });
    }
  });
});

router.post('/info/amount', function(req, res){
  var data = req.body;
  user.checkToken(data.token, function(errorCode){
    if(ec.getResult(errorCode)){
      info.getAllAmount(function(errorCode, result){
        var response = ec.addErrorInfo(errorCode, result);
        res.send(response);
      });
    }else{
      var response = ec.addErrorInfo(errorCode);
      res.send(response);
    }
  });
});

module.exports = router;
